package com.shuai.chatroom.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns(("/user/register"))
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/reg.html")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/user/captcha")
                .excludePathPatterns("/user/up_photo");
    }

    @Value("${user_photos_path}")
    private String rootPath;

    @Value("${up_file_path}")
    private String fileRootPath;

    /**
     * static/img路径下静态资源路径映射。我把资源都放在img下了，关闭这个映射，方便老师测试
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("img/**")
                .addResourceLocations("file:" + rootPath);
        registry.addResourceHandler("file/**")
                .addResourceLocations("file:" + fileRootPath);
    }
}
